#include"serialize/treasure.h"

//treasure definition
Treasure::Treasure()
{
//    _id;
//    _name;
//    _description;
//    _max_attack_addon_min;
//    _max_attack_addon_max;
//    _max_defense_addon_min;
//    _max_defense_addon_max;
//    _max_attack_probability_min;
//    _max_attack_probability_max;
//    _max_defense_probability_min;
//    _max_defense_probability_max;
//    _needed_Fragment_count;
//    _needed_Fragment;

    id = 0;
    name = "";
    description = "";
    max_attack_addon_min = 0;
    max_attack_addon_max = 0;
    min_attack_addon_min = 0;
    min_attack_addon_max = 0;
    max_defense_addon_min = 0;
    max_defense_addon_max = 0;
    min_defense_addon_min = 0;
    min_defense_addon_max = 0;
    max_attack_probability_min = 0;
    max_attack_probability_max = 0;
    max_defense_probability_min = 0;
    max_defense_probability_max = 0;
    needed_Fragments_count = 0;
    needed_Fragments = 0;
}

Treasure::Treasure(qint32 _id, QString _name, QString _description, qint8 _max_attack_addon_min, qint8 _max_attack_addon_max, qint8 _min_attack_addon_min, qint8 _min_attack_addon_max, qint8 _max_defense_addon_min, qint8 _max_defense_addon_max, qint8 _min_defense_addon_min, qint8 _min_defense_addon_max, qint8 _max_attack_probability_min, qint8 _max_attack_probability_max, qint8 _max_defense_probability_min, qint8 _max_defense_probability_max, qint32 _needed_Fragments_count, NeededFragment *_needed_Fragments)
{
    id = _id;
    name = _name;
    description = _description;
    max_attack_addon_min = _max_attack_addon_min;
    max_attack_addon_max = _max_attack_addon_max;
    min_attack_addon_min = _min_attack_addon_min;
    min_attack_addon_max = _min_attack_addon_max;
    max_defense_addon_min = _max_defense_addon_min;
    max_defense_addon_max = _max_defense_addon_max;
    min_defense_addon_min = _min_defense_addon_min;
    min_defense_addon_max = _min_defense_addon_max;
    max_attack_probability_min = _max_attack_probability_min;
    max_attack_probability_max = _max_attack_probability_max;
    max_defense_probability_min = _max_defense_probability_min;
    max_defense_probability_max = _max_defense_probability_max;
    needed_Fragments_count = _needed_Fragments_count;
    if(needed_Fragments!=0)
        delete[] needed_Fragments;
    needed_Fragments = new NeededFragment[needed_Fragments_count];
    for(qint32 i = 0; i <needed_Fragments_count;i++)
        needed_Fragments[i] = _needed_Fragments[i];
}

Treasure::Treasure(const Treasure& t)
{
    id = t.id;
    name = t.name;
    description = t.description;
    max_attack_addon_min = t.max_attack_addon_min;
    max_attack_addon_max = t.max_attack_addon_max;
    min_attack_addon_min = t.min_attack_addon_min;
    min_attack_addon_max = t.min_attack_addon_max;
    max_defense_addon_min = t.max_defense_addon_min;
    max_defense_addon_max = t.max_defense_addon_max;
    min_defense_addon_min = t.min_defense_addon_min;
    min_defense_addon_max = t.min_defense_addon_max;
    max_attack_probability_min = t.max_attack_probability_min;
    max_attack_probability_max = t.max_attack_probability_max;
    max_defense_probability_min = t.max_defense_probability_min;
    max_defense_probability_max = t.max_defense_probability_max;
    needed_Fragments_count = t.needed_Fragments_count;
    if(needed_Fragments!=0)
        delete[] needed_Fragments;
    needed_Fragments = new NeededFragment[needed_Fragments_count];
    for(qint32 i = 0; i <needed_Fragments_count;i++)
        needed_Fragments[i] = t.needed_Fragments[i];
}

Treasure& Treasure::operator=(const Treasure& t)
{
    if(&t == this)
        return *this;

    id = t.id;
    name = t.name;
    description = t.description;
    max_attack_addon_min = t.max_attack_addon_min;
    max_attack_addon_max = t.max_attack_addon_max;
    min_attack_addon_min = t.min_attack_addon_min;
    min_attack_addon_max = t.min_attack_addon_max;
    max_defense_addon_min = t.max_defense_addon_min;
    max_defense_addon_max = t.max_defense_addon_max;
    min_defense_addon_min = t.min_defense_addon_min;
    min_defense_addon_max = t.min_defense_addon_max;
    max_attack_probability_min = t.max_attack_probability_min;
    max_attack_probability_max = t.max_attack_probability_max;
    max_defense_probability_min = t.max_defense_probability_min;
    max_defense_probability_max = t.max_defense_probability_max;
    needed_Fragments_count = t.needed_Fragments_count;
    if(needed_Fragments!=0)
        delete[] needed_Fragments;
    needed_Fragments = new NeededFragment[needed_Fragments_count];
    for(qint32 i = 0; i <needed_Fragments_count;i++)
        needed_Fragments[i] = t.needed_Fragments[i];
    return *this;
}

Treasure::~Treasure()
{
    if(needed_Fragments_count!=0)
        delete[] needed_Fragments;
}

ostream& operator <<(ostream& os,Treasure& t)
{
    os<<"******start******"<<endl;
    os<<"Treasure info:"<<endl;
    os<<"id:"<<(int)t.id<<endl;
    os<<"name:"<<t.name.toStdString()<<endl;
    os<<"descriptionion:"<<t.description.toStdString()<<endl;

    os<<"max_attack_addon_min:"<<(int)t.max_attack_addon_min<<endl;
    os<<"max_attack_addon_max:"<<(int)t.max_attack_addon_max<<endl;
    os<<"min_attack_addon_min:"<<(int)t.min_attack_addon_min<<endl;
    os<<"min_attack_addon_max:"<<(int)t.min_attack_addon_max<<endl;
    os<<"max_defense_addon_min:"<<(int)t.max_defense_addon_min<<endl;
    os<<"max_defense_addon_max:"<<(int)t.max_defense_addon_max<<endl;
    os<<"min_defense_addon_min:"<<(int)t.min_defense_addon_min<<endl;
    os<<"min_defense_addon_max:"<<(int)t.min_defense_addon_max<<endl;
    os<<"max_attack_probability_min:"<<(int)t.max_attack_probability_min<<endl;
    os<<"max_attack_probability_max:"<<(int)t.max_attack_probability_max<<endl;
    os<<"max_defense_probability_min:"<<(int)t.max_defense_probability_min<<endl;
    os<<"max_defense_probability_max:"<<(int)t.max_defense_probability_max<<endl;

    os<<"needed_Fragments_count:"<<(int)t.needed_Fragments_count<<",They are: ";
    for(qint32 i=0;i<t.needed_Fragments_count;i++)
    {
        os<<"("<<(int)t.needed_Fragments[i].id<<","<<(int)t.needed_Fragments[i].count<<")";
        if(i!=t.needed_Fragments_count-1)
            os<<",";
    }
    os<<endl;

    os<<"******end******"<<endl;

    return os;
}

QDataStream& operator<<(QDataStream& ds,Treasure& t)
{
    ds<<t.id;
    ds<<t.name.toUtf8().data();
    ds<<t.description.toUtf8().data();
    ds<<t.max_attack_addon_min;
    ds<<t.max_attack_addon_max;
    ds<<t.min_attack_addon_min;
    ds<<t.min_attack_addon_max;
    ds<<t.max_defense_addon_min;
    ds<<t.max_defense_addon_max;
    ds<<t.min_defense_addon_min;
    ds<<t.min_defense_addon_max;
    ds<<t.max_attack_probability_min;
    ds<<t.max_attack_probability_max;
    ds<<t.max_defense_probability_min;
    ds<<t.max_defense_probability_max;
    ds<<t.needed_Fragments_count;
    for(qint32 i=0;i<t.needed_Fragments_count;i++)
    {
        ds<<t.needed_Fragments[i].id;
        ds<<t.needed_Fragments[i].count;
    }

    return ds;
}

QDataStream& operator>>(QDataStream& ds,Treasure& t)
{
    char* temp;

    ds>>t.id;
    ds>>temp;
    t.name = QString::fromUtf8(temp);
    delete[] temp;
    ds>>temp;
    t.description = QString::fromUtf8(temp);
    delete[] temp;
    ds>>t.max_attack_addon_min;
    ds>>t.max_attack_addon_max;
    ds>>t.min_attack_addon_min;
    ds>>t.min_attack_addon_max;
    ds>>t.max_defense_addon_min;
    ds>>t.max_defense_addon_max;
    ds>>t.min_defense_addon_min;
    ds>>t.min_defense_addon_max;
    ds>>t.max_attack_probability_min;
    ds>>t.max_attack_probability_max;
    ds>>t.max_defense_probability_min;
    ds>>t.max_defense_probability_max;
    ds>>t.needed_Fragments_count;
    if(t.needed_Fragments!=0)
        delete[] t.needed_Fragments;
    t.needed_Fragments = new NeededFragment[t.needed_Fragments_count];
    for(qint32 i=0;i<t.needed_Fragments_count;i++)
    {
        ds>>t.needed_Fragments[i].id;
        ds>>t.needed_Fragments[i].count;
    }

    return ds;
}
